package com.junchenrobot.mes.schedule;

import com.junchenrobot.mes.core.TaskScheduled;
import com.junchenrobot.mes.dao.AgvWorkDao;
import com.junchenrobot.mes.domain.AgvWork;
import com.junchenrobot.mes.domain.FindDevice;
import com.junchenrobot.mes.domain.WinderTaskSpare;
import com.junchenrobot.mes.listener.impl.DefaultInputTaskImpl;
import com.junchenrobot.mes.listener.impl.DefaultInputTaskSrc;
import com.junchenrobot.mes.listener.moudle.InputTaskEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

@Slf4j
@Component
public class InputTaskSchedule {
    
    private final AgvWorkDao agvWorkDao;
    
    private final DefaultInputTaskSrc defaultInputTaskSrc;

    private final TaskScheduled taskScheduled;

    @Autowired
    public InputTaskSchedule(AgvWorkDao agvWorkDao, TaskScheduled taskScheduled) {
        this.agvWorkDao = agvWorkDao;
        this.taskScheduled = taskScheduled;
        defaultInputTaskSrc = new DefaultInputTaskSrc();

        defaultInputTaskSrc.addListener(new DefaultInputTaskImpl(taskScheduled));
    }
    
    @Scheduled(cron = "*/20 * * * * ?")
    public void TaskSchedule() {
        log.info("                                           ");
        log.info("------------------备用表更新--------------------");
        // 批量查询任务列表
        List<AgvWork> agvWorks = agvWorkDao.listAgvWork();
        if (!Objects.isNull(agvWorks) && agvWorks.size() > 0) {
            log.info("查询到卷绕机数据库任务有共有:{}条",agvWorks.size());
            for (AgvWork agvWork:agvWorks){
                log.info("卷绕机数据库任务有:{}",agvWork);
            }
            InputTaskEvent inputTaskEvent = new InputTaskEvent();
            inputTaskEvent.setWorkList(agvWorks);
            defaultInputTaskSrc.notifyListener(inputTaskEvent);
        }
    }

    @Scheduled(cron = "*/3 * * * * ?")
    public void vehicleStatus()  {
        taskScheduled.getVehicleStatusList();
    }


    @Scheduled(cron = "*/3 * * * * ?")
    public void AssignTasks11()  {
        long before = System.currentTimeMillis();
        log.info("-/**-");
        String deviceName = "1";
        //获取设备状态分派任务
          taskScheduled.deviceTask(deviceName);
         deviceName = "2";
        //获取设备状态分派任务
        taskScheduled.deviceTask(deviceName);
         deviceName = "3";
        //获取设备状态分派任务
        taskScheduled.deviceTask(deviceName);
        long later = System.currentTimeMillis();
        if ((later - before) > 1000){
            log.info("总耗时:{}",later - before);
        }
    }
    
    /**
     * 更新过时任务0
     */
    @Scheduled(cron = "0 0/3 * * * ?")
    public void updateTask() {
        // 更新的结果数量
        int resultCount = agvWorkDao.replaceAgvStatus(7, 4);
        log.info("updated Count {}", resultCount);
    }
    
}
